package com.ys.controller.api.sys;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.sys.YsGrid;
import com.ys.enume.sys.YsGridColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.YsGridColService;
import com.ys.service.sys.YsGridService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/ysGrid")
public class ApiInterYsGridController {
	
	@Autowired
	private CommonApiService<YsGrid> commonApiService;
	@Autowired
	private YsGridService ysGridService;
	@Autowired
	private YsGridColService ysGridColService;

	@RequiresPermissions("ysGrid:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute YsGrid cond
			){
		
		return commonApiService.searchLay(cond);
	}
	
	@RequestMapping("/searchForModalSelector")
    @ResponseBody
	public LayuiTableVoJA searchForModalSelector(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute YsGrid cond
			){
		
		return commonApiService.searchLay(cond);
	}
	
	@RequestMapping("/dtree")
    @ResponseBody
	public DtreeSqlDto dtree(Model model, HttpServletRequest request, HttpServletResponse response){
		return ysGridService.dtree();
	}
	
	@RequestMapping("/dtreeForGridButton")
    @ResponseBody
	public DtreeSqlDto dtreeForGridButton(Model model, HttpServletRequest request, HttpServletResponse response){
		return ysGridService.dtree();
	}
	
	/*
	 * 彻底删除
	 */
	@PostMapping("/deleteTrue")
	@ResponseBody
	public R deleteTrue(HttpServletRequest request, HttpServletResponse response,
			Long id) {
		return ysGridService.deleteTrue(id);
	}
	
	/**
	 * 新增
	 */
	@RequiresPermissions("ysGrid:add")
	@PostMapping("/add")
    @ResponseBody
	public R add(HttpServletRequest request, @RequestParam Map<String, Object> params){
		return ysGridService.add(params);
	}
	
	/**
	 * 修改
	 */
	@RequiresPermissions("ysGrid:edit")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(HttpServletRequest request, @RequestParam Map<String, Object> params){
		return ysGridService.edit(params);
	}
	
	/**
	 * 调整顺序
	 */
	@PostMapping("/resort")
    @ResponseBody
	public R resort(Model model, HttpServletRequest request, HttpServletResponse response,
			String rows){
		return ysGridService.resort(rows);
	}
	
	/**
	 * 设置
	 */
	@PostMapping("/setting")
    @ResponseBody
	public R setting(HttpServletRequest request,
			Long id,
			String nameShiroAuth,
			String templetType,
			String pathSelfDefine,
			String pathGridRequest, String pathGridEditRequest, String pathGridExportRequest,
			String pathCommitAdd, String pathCommitEdit, String pathCommitDelete,
			Long flowModelId, String pathFlowPass, String pathFlowRefuse,
			String preCode, String connectCode, Integer startNo){
		return ysGridService.setting(id, nameShiroAuth, templetType, pathSelfDefine, pathGridRequest, pathGridEditRequest, pathGridExportRequest,
				pathCommitAdd, pathCommitEdit, pathCommitDelete,
				flowModelId, pathFlowPass, pathFlowRefuse,
				preCode, connectCode, startNo);
	}
	
	/**
	 * 批量新增字段
	 */
	@PostMapping("/batchAddCol")
	@ResponseBody
	public R batchAddCol(HttpServletRequest request,
			String subData){
		
		JSONArray detailJA = JSON.parseArray(subData);
		for (int i = 0; i < detailJA.size(); i++) {
			JSONObject JO = detailJA.getJSONObject(i);
			Long gridId = JO.getLong("gridId");
			String code = JO.getString("code");
			String name = JO.getString("name");
			Integer frontWidth = 160;
			Integer frontAlign = YsGridColEnum.FrontAlign.CENTER.value;
			Integer sortNo = 999;
			String frontType = YsGridColEnum.FrontType.TEXT.value;
			Integer frontEdit = YsGridColEnum.FrontEdit.NO.value;
			Integer frontShow = YsGridColEnum.FrontShow.SHOW.value;
			String dictKeyBase = null;
			String modalKey = null;
			String modalRelativeCode = null;
			String modalValueCode = null;
			Integer multyFileSourceType = null;
			ysGridColService.add(gridId, name, code, sortNo, frontType, frontEdit, frontWidth, frontAlign, frontShow, dictKeyBase, modalKey, modalRelativeCode, modalValueCode, multyFileSourceType);
		}
		return R.ok();
	}
	
}
